import axios from "axios";

export default {
  state: {
    newsList: [],
  },
  mutations: {
    SET_NEWS_LIST(state, data) {
      // state.newsList = data
      // state.newsList = [...state.newsList, ...data];
      state.newsList = state.newsList.concat(data);
    },
  },
  actions: {
    async createNewsAction(context, data) {
      const res = await axios({
        method: "POST",
        url: "http://localhost:3000/news",
        data,
      });
      return res;
    },
    async updateNewsAction(context, { data, id }) {
      const res = await axios({
        method: "PUT",
        url: `http://localhost:3000/news/${id}`,
        data,
      });
      return res;
    },
    async getNewsAction(context, id) {
      const res = await axios({
        url: `http://localhost:3000/news/${id}`,
      });
      return res;
    },
    async getNewsListAction(context, { page }) {
      const res = await axios({
        url: `http://localhost:3000/news?_limit=4&_page=${page}`,
      });
      context.commit("SET_NEWS_LIST", res.data)
    },
  },
  namespaced: true,
};
